using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ctsu.Admin.Forms.CoreLab;

using Syncfusion.Windows.Forms.Grid;
using Syncfusion.Windows.Forms.Tools;
using ctsu.Admin.Forms;
using ctsu.BaseForms;
using ctsu.Security;
using ctsu.Shared;
using ctsu.Data;

namespace ctsu.Admin.BusLogic.CoreLab
{
	class BLFormTestReferenceEdit : BLCoreLabFormBaseImpl
	{
		#region control references
		private GridControl _testReferenceItems;
		private TextBox _batchNum;
		private TextBox _notes;

		private Button _btnEditBatch;
		private Button _btnClear;
		private Button _btnRemoveBatch;
		private Button _btnSelect;
		private Button _btnPrint;

		private DateTimePickerAdv _outDate;

		//location
		private ComboBox _freezer;
		private NumericUpDown _shelf;
		private NumericUpDown _rack;
		private NumericUpDown _box;
		#endregion

		public BLFormTestReferenceEdit(FormMain formMain, FormBase form, FormInfoBase formInfo, DataTable tableSchema, Permission formSecurity, Hashtable configData)
			: base(formMain, form, formInfo, tableSchema, formSecurity, configData)
		{

		}

		#region initialize
		protected override void _vcManager_OnAfterHookEvents()
		{
			_testReferenceItems = (GridControl)Forms.Utils.GetControlByName("TestReferenceItems", this._form.Controls);
			_batchNum = (TextBox)Forms.Utils.GetControlByName("ID", this._form.Controls);
			_notes = (TextBox)Forms.Utils.GetControlByName("Notes", this._form.Controls);

			_btnEditBatch = (Button)Forms.Utils.GetControlByName("btnEditBatch", this._form.Controls);
			_btnClear = (Button)Forms.Utils.GetControlByName("btnClear", this._form.Controls);
			_btnRemoveBatch = (Button)Forms.Utils.GetControlByName("btnRemoveBatch", this._form.Controls);
			_btnSelect = (Button)Forms.Utils.GetControlByName("btnSelect", this._form.Controls);
			_btnPrint = (Button)Forms.Utils.GetControlByName("btnPrint", this._form.Controls);

			_outDate = (DateTimePickerAdv)Forms.Utils.GetControlByName("OutDate", this._form.Controls);

			//_location = (TextBox)Forms.Utils.GetControlByName("Location", this._form.Controls);
			_freezer = (ComboBox)Forms.Utils.GetControlByName("Freezer", this._form.Controls);
			_shelf = (NumericUpDown)Forms.Utils.GetControlByName("Shelf", this._form.Controls);
			_rack = (NumericUpDown)Forms.Utils.GetControlByName("Rack", this._form.Controls);
			_box = (NumericUpDown)Forms.Utils.GetControlByName("Box", this._form.Controls);
			_form.btnRemove.Visible = false;
		}

		protected override void SetControlsEnabled(bool enable)
		{
			base.SetControlsEnabled(enable);

			_btnClear.Enabled = enable;
			_btnSelect.Enabled = enable;

			if (enable == false)
			{
				_btnEditBatch.Enabled = enable;
				_btnRemoveBatch.Enabled = enable;
			}

			_outDate.Enabled = enable;
			_outDate.Value = DateTime.Today;
			_outDate.IsNullDate = true;

			_freezer.Enabled = enable;
			_freezer.SelectedIndex = -1;
			_freezer.SelectedIndex = -1;
			_shelf.Enabled = enable;
			_shelf.Value = 0;
			_rack.Enabled = enable;
			_rack.Value = 0;
			_box.Enabled = enable;
			_box.Value = 0;

			_notes.Enabled = enable;
			_notes.Text = "";
		}

		public override void InitializeEmptyControls()
		{
			base.InitializeEmptyControls();
			_freezer.SelectedIndex = -1;
			_freezer.SelectedIndex = -1;
		}
		#endregion initialize

		#region search
		public override void Search(object sender)
		{
			Control ctl = (Control)sender;
			ctl.Enabled = false;

			SearchSamples spd = new SearchSamples();
			spd.ShowDialog(this._form);
			if (spd.SearchChoice == 0)
				base.Search(sender);
			else if (spd.SearchChoice == 1)
				SelectByLabel();

			ctl.Enabled = true;
		}

		private void SelectByLabel()
		{

			Cursor.Current = Cursors.WaitCursor;

			SearchInfo si = (SearchInfo)this.FormInfo.SearchFormInfo["TestReferenceEditBatchSamples"];
			if (si == null)
			{
				MessageBox.Show("Search criteria is not defined.", "Search Criteria Not Fount",
					MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return;
			}

			FormSearch fs = new FormSearch();
			//set the title bar
			//fs.Text = si.Name + " Search";

			//get the search fields			
			foreach (ReportColumn rc in si.SearchFields)
				fs.SearchFields.Items.Add(rc);

			//design list view for result fields			
			ListView lvw = fs.SearchResults;
			lvw.Columns.Clear();
			if (lvw != null)
			{
				foreach (ReportColumn rc in si.SearchResultCols)
				{
					ColumnHeader ch = new ColumnHeader();
					ch.Text = rc.DisplayName;
					ch.Width = -2;
					lvw.Columns.Add(ch);
				}
			}

			SearchSamplesController sc = new SearchSamplesController(fs, si);
			fs.Controller = sc;
			fs.Owner = this._form;
			sc.ItemSelected += new BaseForms.BusinessLogicEvents.BLSelectedItemEventHandler(sc_ItemSelected);
			fs.Closing += new CancelEventHandler(fs_Closing);
			IsSearchVisible = true;
			fs.Show();
		}

		private void sc_ItemSelected(object sender, BLReturnIDEventArgs e)
		{
			if (e.ID > 0)
			{
				Console.WriteLine("Selected ID: " + e.ID.ToString());
				if (GetData(e.ID))
				{
					if (this.FormMode == EditMode.Load)
						SetFormMode(EditMode.Read);
				}
			}
		}

		private void fs_Closing(object sender, CancelEventArgs e)
		{
			IsSearchVisible = false;
			this._form.btnSearch.Enabled = true;
		}
		#endregion search

		public override void GenericClick(Control sender)
		{
			if (sender.Name == "btnSelect")
				SelectSamples();
			if (sender.Name == "btnEditBatch")
				EditBatch();
			if (sender.Name == "btnRemoveBatch")
				RemoveBatch();
			if (sender.Name == "btnClear")
				ClearInfo();
			//if (sender.Name == "btnPrint")
			//    DoPrint();

			if (_testReferenceItems.RowCount == 0)
				this._btnPrint.Enabled = false;
			else
				this._btnPrint.Enabled = true;
		}

		#region select samples
		private void SelectSamples()
		{
			SampleChooser3Dlg dlc = new SampleChooser3Dlg();
			dlc.Owner = this._form;
			dlc.Text = "Select Samples";
			dlc.RdoBatchLabel = "From Test Reference Batch";
			dlc.SelectSampleClick += new ctsu.Admin.Forms.CoreLab.SampleChooser3Dlg.SelectSampleButtonClickEventHandler(dlc_SelectSampleClick);
			dlc.Show();
		}

		private void dlc_SelectSampleClick(object sender, SelectSamplesEventArgs e)
		{
			if (e.SelectType == 0)
				RetrieveSampleBatch(e.ID);
			else if (e.SelectType == 1)
				RetrieveOneSample(e.ID, true);
			else
				RetrieveBox(e.Location);


			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["TestReferenceItems"];
			GridControl grid = fgbl.Grid;
			if (grid.RowCount > 0)
			{
				if (!_btnEditBatch.Enabled)
				{
					_btnEditBatch.Enabled = true;
					_btnRemoveBatch.Enabled = true;
					_btnPrint.Enabled = true;
				}
			}
		}

		private void RetrieveOneSample(int id, bool displayMessage)
		{
			StringBuilder sb = new StringBuilder();

			if (IsInList(id))
			{
				if (displayMessage)
				{
					sb.Append("Sample ID: " + id + " is already in the list");
					MessageBox.Show(this._form, sb.ToString(), "Duplicate ID",
						MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				return;
			}

			string sql = "SELECT r.ID, r.OutDate, r.Location, r.Notes, " +
				" d.Protocol, d.Patient, tt.Display, st.display as Sample, t.Result " +
				" FROM TestReference r " +
				"INNER JOIN TestSamples t ON t.ID = r.ID " +
				"INNER JOIN DrawSamples d ON t.DrawSample = d.ID " +
				"INNER JOIN TestType_L tt ON t.TestType = tt.ID " +
				"INNER JOIN SampleType_L st ON d.SampleType = st.ID " +
				" WHERE r.ID =" + id;

			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["TestReferenceItems"];
			GridControl grid = fgbl.Grid;

			//SqlConnection conn = LoginForm.GetSqlConnection();			
			IDataReader reader = null;
			try
			{
				reader = DataAccess.GetDataReader(sql);

				Sample samp = null;
				string message = "";

				grid.BeginUpdate();
				if (reader.Read())
				{
					Cursor.Current = Cursors.WaitCursor;
					samp = new Sample();
					samp.TestID = reader.GetInt32(0);
					if (!reader.IsDBNull(1))
						samp.OutDate = reader.GetDateTime(1);
					if (!reader.IsDBNull(2))
						samp.Location = reader.GetString(2);
					if (!reader.IsDBNull(3))
						samp.Note = reader.GetString(3);
					samp.Protocol = reader.GetInt32(4);
					//get protocolInfo
					samp.ProtocolInfo = CoreLabUtils.GetProtocolInfo(samp.Protocol, out message);

					samp.PatientID = reader.GetInt32(5);
					//get the patient info
					samp.PatientInfo = CoreLabUtils.GetPatientInfo(samp.PatientID, out message);

					samp.TestName = reader.GetString(6);
					samp.SampleName = reader.GetString(7);
					if (!reader.IsDBNull(8))
						samp.Result = reader.GetString(8);
					fgbl.AddRow(samp, 0);
				}
				else
				{
					sb.Append("Sample ID: " + id + " could not be found or is checked out.");
					MessageBox.Show(this._form, sb.ToString(), "Sample Not Found",
						MessageBoxButtons.OK, MessageBoxIcon.Information);
					reader.Close();
					return;
				}
			}
			catch (Exception ex)
			{
				sb.Append("Could not retrieve sample!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(this._form, sb.ToString(), "Get Sample Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			finally
			{
				if (reader != null)
				{
					if (!reader.IsClosed)
						reader.Close();
				}
				//if(conn != null)
				//    conn.Dispose();
				grid.EndUpdate();
			}
		}

		private void RetrieveSampleBatch(int num)
		{
			StringBuilder sb = new StringBuilder();
			string sql = "SELECT r.ID, r.OutDate, r.Location, r.Notes, " +
				" d.Protocol, d.Patient, tt.Display, st.display as Sample, t.Result " +
				" FROM TestReference r " +
				"INNER JOIN TestSamples t ON t.ID = r.ID " +
				"INNER JOIN DrawSamples d ON t.DrawSample = d.ID " +
				"INNER JOIN TestType_L tt ON t.TestType = tt.ID " +
				"INNER JOIN SampleType_L st ON d.SampleType = st.ID " +
				" WHERE r.BatchNum =" + num +
				" ORDER BY r.ID";

			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["TestReferenceItems"];
			GridControl grid = fgbl.Grid;

			//SqlConnection conn = LoginForm.GetSqlConnection();
			IDataReader reader = null;
			try
			{

				reader = DataAccess.GetDataReader(sql);
				Sample samp = null;
				string message = "";

				if (reader.Read())
				{
					Cursor.Current = Cursors.WaitCursor;
					grid.BeginUpdate();
					do
					{
						samp = new Sample();
						samp.TestID = reader.GetInt32(0);
						if (!reader.IsDBNull(1))
							samp.OutDate = reader.GetDateTime(1);
						if (!reader.IsDBNull(2))
							samp.Location = reader.GetString(2);
						if (!reader.IsDBNull(3))
							samp.Note = reader.GetString(3);
						samp.Protocol = reader.GetInt32(4);
						//get protocolInfo
						samp.ProtocolInfo = CoreLabUtils.GetProtocolInfo(samp.Protocol, out message);

						samp.PatientID = reader.GetInt32(5);
						//get the patient info
						samp.PatientInfo = CoreLabUtils.GetPatientInfo(samp.PatientID, out message);

						samp.TestName = reader.GetString(6);
						samp.SampleName = reader.GetString(7);
						if (!reader.IsDBNull(8))
							samp.Result = reader.GetString(8);
						fgbl.AddRow(samp, 0);
					}
					while (reader.Read());
				}
				else
				{
					sb.Append("Batch was not found or all samples are checked out.");
					MessageBox.Show(this._form, sb.ToString(), "Draw Not Found",
						MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
			catch (Exception ex)
			{
				sb.Append("Could not retrieve batch!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(this._form, sb.ToString(), "Retrieve Batch Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			finally
			{
				if (reader != null)
				{
					if (!reader.IsClosed)
						reader.Close();
				}
				//if(conn != null)
				//    conn.Dispose();
				grid.EndUpdate();
			}
		}

		private void RetrieveBox(string location)
		{
			StringBuilder sb = new StringBuilder();
			string sql = "SELECT r.ID, r.OutDate, r.Location, r.Notes, " +
				" d.Protocol, d.Patient, tt.Display, st.display as Sample, t.Result " +
				" FROM TestReference r " +
				"INNER JOIN TestSamples t ON t.ID = r.ID " +
				"INNER JOIN DrawSamples d ON t.DrawSample = d.ID " +
				"INNER JOIN TestType_L tt ON t.TestType = tt.ID " +
				"INNER JOIN SampleType_L st ON d.SampleType = st.ID " +
				" WHERE r.Location ='" + location + "'" +
				" ORDER BY r.ID";

			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)_gridBLs["TestReferenceItems"];
			GridControl grid = fgbl.Grid;

			//SqlConnection conn = LoginForm.GetSqlConnection();
			IDataReader reader = null;
			try
			{

				reader = DataAccess.GetDataReader(sql);
				Sample samp = null;
				string message = "";

				if (reader.Read())
				{
					Cursor.Current = Cursors.WaitCursor;
					grid.BeginUpdate();
					do
					{
						samp = new Sample();
						samp.TestID = reader.GetInt32(0);
						if (!reader.IsDBNull(1))
							samp.OutDate = reader.GetDateTime(1);
						if (!reader.IsDBNull(2))
							samp.Location = reader.GetString(2);
						if (!reader.IsDBNull(3))
							samp.Note = reader.GetString(3);
						samp.Protocol = reader.GetInt32(4);
						//get protocolInfo
						samp.ProtocolInfo = CoreLabUtils.GetProtocolInfo(samp.Protocol, out message);

						samp.PatientID = reader.GetInt32(5);
						//get the patient info
						samp.PatientInfo = CoreLabUtils.GetPatientInfo(samp.PatientID, out message);

						samp.TestName = reader.GetString(6);
						samp.SampleName = reader.GetString(7);
						if (!reader.IsDBNull(8))
							samp.Result = reader.GetString(8);
						fgbl.AddRow(samp, 0);
					}
					while (reader.Read());
				}
				else
				{
					sb.Append("Batch was not found.");
					MessageBox.Show(this._form, sb.ToString(), "Draw Not Found",
						MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
			catch (Exception ex)
			{
				sb.Append("Could not retrieve batch!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(this._form, sb.ToString(), "Retrieve Batch Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			finally
			{
				if (reader != null)
				{
					if (!reader.IsClosed)
						reader.Close();
				}
				//if(conn != null)
				//    conn.Dispose();
				grid.EndUpdate();
			}
		}

		private bool IsInList(int id)
		{
			GridControl grid = (GridControl)Forms.Utils.GetControlByName("TestReferenceItems", this._form.Controls);
			GridStyleInfo gsi = null;

			if (grid.RowCount == 0)
				return false;

			for (int i = 1; i <= grid.RowCount; i++)
			{
				gsi = grid[i, 1];
				if (gsi.Text.Length > 0)
					if (Convert.ToInt32(gsi.CellValue) == id)
						return true;
			}
			return false;
		}
		#endregion select samples

		#region get,display,clear
		private void ClearInfo()
		{
			ClearLocation();
			this._notes.Text = "";
			this._outDate.IsNullDate = true;
		}
		private void ClearLocation()
		{
			_freezer.SelectedIndex = -1;
			_freezer.SelectedIndex = -1;
			this._shelf.Value = 0;
			this._rack.Value = 0;
			this._box.Value = 0;
		}
		protected override void grd_CellDoubleClick(object sender, GridCellClickEventArgs e)
		{
			if (e.RowIndex < 1)
				return;
			if (this.FormMode == EditMode.Edit || this.FormMode == EditMode.New)
				FillControlsWithRowData(e.RowIndex);
		}
		private void FillControlsWithRowData(int row)
		{
			GridControl grid = ((BLCoreLabFormGridBaseImpl)this._gridBLs["TestReferenceItems"]).Grid;

			string sVal = grid[row, 4].Text;
			DisplayLocationInControls(sVal);

			if (grid[row, 8].Text.Length == 0)
				this._outDate.IsNullDate = true;
			else
				this._outDate.Value = DateTime.Parse(grid[row, 8].Text);

			this._notes.Text = grid[row, 9].Text;
		}
		private void DisplayLocationInControls(string location)
		{
			if (location.Length == 0)
			{
				ClearLocation();
				return;
			}

			string[] a = location.Split(',');
			foreach (string s in a)
			{
				if (s.StartsWith("Fz"))
				{
					this._freezer.SelectedValue = Convert.ToInt32(s.Substring(3));
				}
				if (s.StartsWith("Sf"))
				{
					this._shelf.Value = Convert.ToInt32(s.Substring(3));
				}
				if (s.StartsWith("Rk"))
				{
					this._rack.Value = Convert.ToInt32(s.Substring(3));
				}
				if (s.StartsWith("Bx"))
				{
					this._box.Value = Convert.ToInt32(s.Substring(3));
				}
			}
		}
		#endregion get,display,clear

		#region edit batch
		private string GetFormatatedLocation()
		{
			if (_freezer.SelectedIndex == -1)
				return "";

			string location = "Fz:" + this._freezer.SelectedValue.ToString() + "," +
				"Sf:" + this._shelf.Value.ToString() + "," +
				"Rk:" + this._rack.Value.ToString() + "," +
				"Bx:" + this._box.Value.ToString();
			return location;
		}

		private void RemoveBatch()
		{
			GridControl grid = (GridControl)Forms.Utils.GetControlByName("TestReferenceItems", this._form.Controls);

			GridRangeInfoList list;
			list = grid.Selections.GetSelectedRows(true, false);
			if (list.Count == 0)
				return;

			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)this._gridBLs["TestReferenceItems"];

			ArrayList alRows = new ArrayList();
			int row = 0;
			int top = 0;
			int bottom = 0;
			for (int i = 0; i < list.Count; i++)
			{
				top = list[i].Top;
				bottom = list[i].Bottom;
				if (bottom > top)
					GetSelectedRows(alRows, top, bottom);
				else
					alRows.Add(list[i].Top);
			}
			for (int i = 0; i < alRows.Count; i++)
			{
				row = (int)alRows[i];
				row -= i;
				DataRow dr = (DataRow)fgbl.Grid[row, 0].Tag;
				dr.RejectChanges();
				fgbl.RemoveRow(row);
			}

			if (grid.RowCount == 0)
			{
				_btnRemoveBatch.Enabled = false;
				_btnEditBatch.Enabled = false;
				_btnPrint.Enabled = false;
			}

			this._form.btnSave.Enabled = true;
		}

		private void GetSelectedRows(ArrayList alRows, int top, int bottom)
		{
			int rows = bottom - top + 1;
			for (int i = 0; i < rows; i++)
				alRows.Add(top + i);
		}

		private void EditBatch()
		{
			this._btnEditBatch.Enabled = false;
			TestReferenceEditDlg ebd = new TestReferenceEditDlg();
			//			if(!ebd.Initialize())
			//				return;
			ebd.ButtonClick += new ctsu.Admin.Forms.CoreLab.TestReferenceEditDlg.ButtonClickEventHandler(ebd_ButtonClick);
			ebd.Closing += new CancelEventHandler(ebd_Closing);
			ebd.Owner = this._form;
			ebd.Show();
			//ebd.ClearControls();
		}

		private void ebd_ButtonClick(object sender, EditBatchEventArgs e)
		{
			if (e.ButtonName == "ReplaceSelected")
				EditReplaceSelected(e.ReplaceColumns, e.ColumnMatch);
			if (e.ButtonName == "ReplaceAll")
				EditReplaceAll(e.ReplaceColumns, e.ColumnMatch);
		}

		private void ebd_Closing(object sender, CancelEventArgs e)
		{
			TestReferenceEditDlg ebd = (TestReferenceEditDlg)sender;
			ebd.ButtonClick -= new TestReferenceEditDlg.ButtonClickEventHandler(ebd_ButtonClick);
			ebd.Closing -= new CancelEventHandler(ebd_Closing);
			if (this._formMode == EditMode.Edit || this._formMode == EditMode.New)
				this._btnEditBatch.Enabled = true;
		}

		private void EditReplaceAll(bool[] replaceColumns, Hashtable columnMatch)
		{
			Sample samp = new Sample();
			if (!SetReplacementColumns(replaceColumns, ref samp))
				return;

			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)this._gridBLs["TestReferenceItems"];
			ArrayList al = new ArrayList();
			al.Add(replaceColumns);
			al.Add(samp);

			for (int i = 1; i <= fgbl.Grid.RowCount; i++)
				fgbl.ReplaceRow(al, i);
			this._form.btnSave.Enabled = true;
		}

		private void EditReplaceSelected(bool[] replaceColumns, Hashtable columnMatch)
		{
			GridControl grid = (GridControl)Forms.Utils.GetControlByName("TestReferenceItems", this._form.Controls);

			GridRangeInfoList list;
			list = grid.Selections.GetSelectedRows(true, false);

			Sample samp = new Sample();
			if (!SetReplacementColumns(replaceColumns, ref samp))
				return;

			BLCoreLabFormGridBaseImpl fgbl = (BLCoreLabFormGridBaseImpl)this._gridBLs["TestReferenceItems"];
			ArrayList al = new ArrayList();
			al.Add(replaceColumns);
			al.Add(samp);
			foreach (GridRangeInfo range in list)
			{
				for (int i = range.Top; i <= range.Bottom; i++)
				{
					fgbl.ReplaceRow(al, i);
					this._form.btnSave.Enabled = true;
				}
			}
		}

		private bool SetReplacementColumns(bool[] replaceColumns, ref Sample samp)
		{
			StringBuilder sb = new StringBuilder();
			if (replaceColumns[0])
			{
				if (!VerifyAdd())
					return false;

				if (_outDate.IsNullDate)
				{
					samp.OutDate = CremaConstants.NullDate;
					samp.Location = GetFormatatedLocation();
				}
				else
				{
					samp.OutDate = _outDate.Value;
					samp.Location = "";
				}

				samp.Note = _notes.Text;
			}
			if (replaceColumns[1])
			{
				if (!VerifyLocation())
				{
					sb.Append("You must enter a valid location!");
					MessageBox.Show(this._form, sb.ToString(), "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_freezer.Focus();
					return false;
				}
				samp.Location = GetFormatatedLocation();
			}

			if (replaceColumns[2])
			{
				if (_outDate.IsNullDate)
				{
					sb.Append("You must enter the out date!");
					MessageBox.Show(this._form, sb.ToString(), "Out Date", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					_outDate.Focus();
					return false;
				}
				samp.OutDate = this._outDate.Value;
			}

			if (replaceColumns[3])
			{
				samp.Note = _notes.Text;
			}

			return true;
		}

		private bool VerifyAdd()
		{
			bool bLocation = VerifyLocation();
			if ((_outDate.IsNullDate) && (!bLocation))
			{
				MessageBox.Show(this._form, "You must enter a valid location or out date.", "Invalid Location or Out Date", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return false;
			}

			if ((!_outDate.IsNullDate) && (bLocation))
			{
				MessageBox.Show(this._form, "You cannot enter both location and out date.", "Invalid Location or Out Date", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return false;
			}
			return true;
		}
		private bool VerifyLocation()
		{
			if (this._freezer.SelectedIndex == -1)
				return false;
			if (this._shelf.Value == 0)
				return false;
			if (this._shelf.Value > 9)
				return false;
			if (this._rack.Value > 9)
				return false;
			if (this._box.Value == 0)
				return false;
			return true;
		}
		#endregion edit batch
	}
}
